﻿using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.Collections.Generic;

namespace Huayou.Common
{
    [Serializable]
    public class ExtendedAttributes
    {


        public ExtendedAttributes()
        {
            //
            // TODO: Add constructor logic here
            //
        }


        string defaultSettingKeyName = "defaultSettingKeyForSystem";
        Dictionary<string, NameValueCollection> extendedAttributesDic = new Dictionary<string, NameValueCollection>();
        NameValueCollection extendedAttributes
        {
            get
            {
                if (extendedAttributesDic.ContainsKey(defaultSettingKeyName) == false)
                {
                    NameValueCollection extendedAttributes = new NameValueCollection();
                    extendedAttributesDic.Add(defaultSettingKeyName, extendedAttributes);
                }

                return extendedAttributesDic[defaultSettingKeyName];
            }
        }

        public string GetExtendedAttribute(string name)
        {
            return GetExtendedAttribute(string.Empty, name);
        }

        public string GetExtendedAttribute(string settingKey, string name)
        {
            if (string.IsNullOrEmpty(settingKey))
            {
                settingKey = defaultSettingKeyName;
            }

            if (extendedAttributesDic.ContainsKey(settingKey))
            {
                NameValueCollection nvc = extendedAttributesDic[settingKey];
                if (nvc != null)
                {
                    string returnValue = nvc[name];

                    if (returnValue == null)
                    {
                        return string.Empty;
                    }
                    else
                    {
                        return returnValue;
                    }
                }
                else
                {
                    return string.Empty;
                }
            }
            else
            {
                return string.Empty;
            }
        }

        public virtual void SetExtendedAttribute(string name, string value)
        {
            SetExtendedAttribute(string.Empty, name, value);
        }

        public virtual void SetExtendedAttribute(string settingKey, string name, string value)
        {
            if (string.IsNullOrEmpty(settingKey))
            {
                settingKey = defaultSettingKeyName;
            }

            if (extendedAttributesDic.ContainsKey(settingKey) == false)
            {
                NameValueCollection extendedAttributes = new NameValueCollection();
                extendedAttributesDic.Add(settingKey, extendedAttributes);
            }
            NameValueCollection nvc = extendedAttributesDic[settingKey];
            if (nvc != null)
            {
                if ((value == null) || (value == string.Empty))
                {
                    nvc.Remove(name);
                }
                else
                {
                    nvc[name] = value;
                }
            }
        }

        public int GetExtendedAttributesCount()
        {
            return GetExtendedAttributesCount(string.Empty);
        }

        public int GetExtendedAttributesCount(string settingKey)
        {
            if (string.IsNullOrEmpty(settingKey))
            {
                settingKey = defaultSettingKeyName;
            }

            NameValueCollection nvc = extendedAttributesDic[settingKey];
            if (nvc == null)
            {
                return 0;
            }
            else
            {
                return nvc.Count;
            }
        }

        public virtual T Get<T>(string name)
        {
            return Get<T>(string.Empty, name);
        }

        public virtual T Get<T>(string settingKey, string name)
        {
            T t;
            string b = GetExtendedAttribute(settingKey, name);
            if (string.IsNullOrEmpty(b))
            {
                return default(T);
            }
            else
            {
                t = (T)Convert.ChangeType(b, typeof(T));
            }


            return t;
        }

        //public bool GetBool(string name, bool defaultValue)
        //{
        //    string b = GetExtendedAttribute(name);
        //    if (b == null || b.Trim().Length == 0)
        //        return defaultValue;
        //    try
        //    {
        //        return bool.Parse(b);
        //    }
        //    catch { }
        //    return defaultValue;
        //}

        //public int GetInt(string name, int defaultValue)
        //{
        //    string i = GetExtendedAttribute(name);
        //    if (i == null || i.Trim().Length == 0)
        //        return defaultValue;

        //    return Int32.Parse(i);
        //}

        //public DateTime GetDateTime(string name, DateTime defaultValue)
        //{
        //    string d = GetExtendedAttribute(name);
        //    if (d == null || d.Trim().Length == 0)
        //        return defaultValue;

        //    return DateTime.Parse(d);
        //}

        //public string GetString(string name, string defaultValue)
        //{
        //    string v = GetExtendedAttribute(name);
        //    return (string.IsNullOrEmpty(v)) ? defaultValue : v;
        //}



        #region Serialization
        public SerializerData EncodeSerializerData()
        {
            return EncodeSerializerData(string.Empty);
        }

        public SerializerData EncodeSerializerData(string settingKey)
        {
            if (string.IsNullOrEmpty(settingKey))
            {
                settingKey = defaultSettingKeyName;
            }

            SerializerData data = new SerializerData();

            string keys = null;
            string values = null;

            NameValueCollection nvc = extendedAttributesDic[settingKey];
            if (nvc != null)
            {
                Serializer.ConvertFromNameValueCollection(nvc, ref keys, ref values);
                data.Keys = keys;
                data.Values = values;
            }

            return data;
        }

        public void ParseSerializerData(string settingKey, SerializerData data)
        {
            NameValueCollection nvc;
            if (extendedAttributesDic.ContainsKey(settingKey) == false)
            {
                nvc = new NameValueCollection();
                extendedAttributesDic.Add(settingKey, nvc);
            }
            else
            {
                nvc = extendedAttributesDic[settingKey];
            }

            nvc = Serializer.ConvertToNameValueCollection(data.Keys, data.Values);
            extendedAttributesDic[settingKey] = nvc;

            if (nvc == null)
            {
                nvc = new NameValueCollection();
            }

        }
        #endregion
    }
}